package com.chhuang.utils.log;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;

/**
 * log4j日志工具类
 * @author 程秀超
 * @date 2013-4-2
 *
 */
public class LogUtil {
	
	/**
	 * 突出显示出错信息
	 * @param log
	 * @param msg
	 */
	public static void error(Log log, String msg) {
		StringBuffer sb = new StringBuffer(
				"*************************************************\n");
		sb.append(msg).append(
				"\n*************************************************");
		log.error(sb);
	}

	/**
	 * 显示警告信息
	 * @param log
	 * @param msg
	 */
	public static void warn(Log log, String msg) {
		StringBuffer sb = new StringBuffer(
				"-------------------------------------------------\n");
		sb.append(msg).append(
				"\n-------------------------------------------------");
		log.error(sb);
	}

	/**
	 * 打印程序耗时
	 * @param log
	 * @param msg
	 * @param beginTime
	 */
	public static void cost(Log log, String msg, Date beginTime) {
		StringBuffer sb = new StringBuffer(msg);
		sb.append(" costs [" + (new Date().getTime() - beginTime.getTime())
				/ 1000.0D + "] seconds");
		log.debug(sb);
	}

	/**
	 * 打印map
	 * @param log
	 * @param map
	 */
	public static void printMap(Log log, Map<String, Object> map) {
		Iterator<String> it = map.keySet().iterator();
		while (it.hasNext()) {
			Object key = it.next();
			log.debug(key + "=" + map.get(key));
		}
	}
}
